<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="auto"
    >
      <el-form-item label="活动标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布者" prop="pushName">
        <el-input
          v-model="queryParams.pushName"
          placeholder="请输入发布者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布时间" prop="pushTime">
        <el-date-picker
          clearable
          v-model="queryParams.pushTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['contest:contest:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contest:contest:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contest:contest:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['contest:contest:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="activityList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="活动标题" align="center" prop="title" />
      <el-table-column
        show-overflow-tooltip
        label="发布者"
        align="center"
        prop="pushName"
      />
      <el-table-column
        show-overflow-tooltip
        label="发布时间"
        align="center"
        prop="pushTime"
      />
      <el-table-column
        show-overflow-tooltip
        label="活动描述"
        align="center"
        prop="description"
      />
      <el-table-column
        label="允许参加的活动人数"
        align="center"
        prop="personNum"
      />
      <!-- <el-table-column label="内容详情" align="center" prop="content" /> -->
      <el-table-column width="200" label="封面" align="center" prop="cover">
        <template slot-scope="scope">
          <img
            :src="scope.row.cover"
            style="max-width: 175px; height: auto"
            alt="封面"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="活动地址"
        align="center"
        prop="local"
      />
      <el-table-column label="是否允许提交电子档" align="center" prop="isEmail">
        <template slot-scope="scope">
          {{ scope.row.isEmail === "0" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="是否收费" align="center" prop="isToll">
        <template slot-scope="scope">
          {{ scope.row.isToll === "0" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="收取费用" align="center" prop="toll" />
      <el-table-column
        label="活动开始时间"
        align="center"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="活动结束时间"
        align="center"
        prop="endDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="主办方"
        align="center"
        prop="organizer"
      />
      <!-- <el-table-column show-overflow-tooltip label="发布者id" align="center" prop="pushId" /> -->
      <!-- <el-table-column label="分类id" align="center" prop="categoryId" /> -->
      <el-table-column
        label="报名开始时间"
        align="center"
        prop="enrollStartDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollStartDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="报名结束时间"
        align="center"
        prop="enrollEndDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollEndDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="已参加人数"
        align="center"
        prop="participationNum"
      />
      <el-table-column label="浏览量" align="center" prop="viewNum" />
      <el-table-column label="是否允许评论" align="center" prop="isComment">
        <template slot-scope="scope">
          {{ scope.row.isComment === "0" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          {{ scope.row.isTop === "0" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.status === '0'" type="success"
            >审核通过</el-tag
          >
          <el-tag v-show="scope.row.status === '1'" type="warning"
            >未经审核</el-tag
          >
          <el-tag v-show="scope.row.status === '2'" type="danger"
            >拒绝通过</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="是否支持团队参加" align="center" prop="isTeam">
        <template slot-scope="scope">
          {{ scope.row.isTeam === "0" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="团队最大数" align="center" prop="teamNum" />
      <el-table-column
        label="操作"
        align="center"
        width="150px"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="checkContestTo(scope.row)"
            v-hasPermi="['contest:contest:edit']"
            >审核</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['contest:contest:edit']">修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['contest:contest:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="活动描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入活动描述" />
        </el-form-item>
        <el-form-item label="允许参加的活动人数" prop="personNum">
          <el-input
            v-model="form.personNum"
            placeholder="请输入允许参加的活动人数"
          />
        </el-form-item>
        <el-form-item label="内容详情">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="图片id" prop="imgId">
          <el-input v-model="form.imgId" placeholder="请输入图片id" />
        </el-form-item>
        <el-form-item label="活动地址" prop="local">
          <el-input v-model="form.local" placeholder="请输入活动地址" />
        </el-form-item>
        <el-form-item label="是否允许提交电子档" prop="isEmail">
          <el-input
            v-model="form.isEmail"
            placeholder="请输入是否允许提交电子档"
          />
        </el-form-item>
        <el-form-item label="是否收费" prop="isToll">
          <el-input v-model="form.isToll" placeholder="请输入是否收费" />
        </el-form-item>
        <el-form-item label="收取费用" prop="toll">
          <el-input v-model="form.toll" placeholder="请输入收取费用" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="startDate">
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endDate">
          <el-date-picker
            clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="主办方" prop="organizer">
          <el-input v-model="form.organizer" placeholder="请输入主办方" />
        </el-form-item>
        <el-form-item label="发布者id" prop="pushId">
          <el-input v-model="form.pushId" placeholder="请输入发布者id" />
        </el-form-item>
        <el-form-item label="分类id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类id" />
        </el-form-item>
        <el-form-item label="报名开始时间" prop="enrollStartDate">
          <el-date-picker
            clearable
            v-model="form.enrollStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="enrollEndDate">
          <el-date-picker
            clearable
            v-model="form.enrollEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="participationNum">
          <el-input
            v-model="form.participationNum"
            placeholder="请输入${comment}"
          />
        </el-form-item>
        <el-form-item label="${comment}" prop="viewNum">
          <el-input v-model="form.viewNum" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isComment">
          <el-input v-model="form.isComment" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isTop">
          <el-input v-model="form.isTop" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="逻辑删除表示" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入逻辑删除表示" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isTeam">
          <el-input v-model="form.isTeam" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="teamNum">
          <el-input v-model="form.teamNum" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listActivity,
  getActivity,
  delActivity,
  addActivity,
  updateActivity,
} from "@/api/activity/activity";

import { findCategoryChildListByName } from "@/api/category/category";

export default {
  name: "Contest",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        pushTime: null,
        pushName: null,
        description: null,
        personNum: null,
        content: null,
        imgId: null,
        local: null,
        isEmail: null,
        isToll: null,
        toll: null,
        startDate: null,
        endDate: null,
        organizer: null,
        pushId: null,
        categoryId: null,
        enrollStartDate: null,
        enrollEndDate: null,
        participationNum: null,
        viewNum: null,
        isComment: null,
        isTop: null,
        status: null,
        isDel: null,
        isTeam: null,
        teamNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //当前页面分类
      categoryList: [],
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },

  //由于需要监听vuex中的数据所以还需要计算属性
  computed: {
    Obj() {
      return this.$store.state.monitor.monitorVariables; // 如果monitor模块启用了命名空间
    },
  },

  //监听器
  watch: {
    Obj(newValue) {
      console.log(123);
      if (newValue) {
        console.log(456);
        //如果监控变量变成了true马上刷新页面 获取最新数据
        this.getList();
      }
    },
  },
  methods: {
    // 发送查询活动的接口 因为当前是活动页面 所以先查询活动然后再查询对应活动下所有的子分类最后 但是这个地方直接用一个接口解决了
    async getCategoryList() {
      const { data } = await findCategoryChildListByName("活动");
      this.categoryList = data;
    },
    /*审核按钮 */
    checkContestTo(value) {
      console.log(123);
      this.$router.push({
        path: "/activitys/checkActivity/",
        query: {
          id: value.id,
        },
      });
    },

    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then((response) => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        personNum: null,
        content: null,
        imgId: null,
        local: null,
        isEmail: null,
        isToll: null,
        toll: null,
        startDate: null,
        endDate: null,
        organizer: null,
        pushId: null,
        categoryId: null,
        enrollStartDate: null,
        enrollEndDate: null,
        participationNum: null,
        viewNum: null,
        isComment: null,
        isTop: null,
        status: null,
        isDel: null,
        isTeam: null,
        teamNum: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getContest(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除活动编号为"' + ids + '"的数据项？')
        .then(function () {
          return delActivity(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "admin/activity/export",
        {
          ...this.queryParams,
        },
        `activity_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
